/**
 * 
 */
package com.testvideo.ui;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JWindow;

import com.testvideo.command.ClipInfoChangeEvent;
import com.testvideo.command.ClipInfoChangeListener;
import com.testvideo.command.IVideoPlayer;
import com.testvideo.command.VideoClipInfo;
import com.testvideo.command.mplayer.BasicVideoPlayer;

/**
 * @author Administrator
 *
 */
public class FleetPlayer {
	
	private final JFrame frame = new JFrame() ;
	final Canvas f=new Canvas();	
	
	private IVideoPlayer videoPlayer ;	
	
	private VideoSizeMonitorThread videoSizeMonitorThread ;
	
	final JPanel top = new JPanel() ;
	final JPanel middle = new JPanel() ;
	PlayerToolBarPanel button = null ;
	
	private JWindow windowForFullScreen ;
	
	int no_cavas_height = 0 ;
	
	public FleetPlayer(){
		setupUI() ;
		setupLogicLinks() ;
	}
	
	public IVideoPlayer getPlayer(){
		return videoPlayer ;
	}
	
	public Canvas getCanvas(){
		return f ;
	}
		
	protected void setupUI() {
		try {
			f.setVisible(true);
			f.setBackground(Color.BLACK);
			JLayeredPane p=new JLayeredPane();
			p.add(f,JLayeredPane.FRAME_CONTENT_LAYER,-40000);
			f.setFocusable(true) ;
			
			windowForFullScreen = new JWindow() ;
//			windowForFullScreen.setVisible(true) ;
//			windowForFullScreen.getContentPane().add(f) ;
			
			frame.setLocation(200, 200);
			frame.setSize(800, 600);
			
			middle.setBackground(Color.BLACK) ;
			middle.setLayout(new BorderLayout()) ;
			middle.add(f, BorderLayout.CENTER) ;
			
			button = new PlayerToolBarPanel(frame) ;
			
			Container cp = frame.getContentPane() ;
			
			cp.setLayout(new BorderLayout()) ;
			cp.add(top, BorderLayout.NORTH) ;
			cp.add(middle, BorderLayout.CENTER) ;
			cp.add(button, BorderLayout.SOUTH) ;
			
			frame.setVisible(true);
			
			no_cavas_height = frame.getHeight() - middle.getHeight() ;
			
			frame.addWindowListener(
					new WindowAdapter(){
						@Override
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
							
							if(videoPlayer != null){
								videoPlayer.quit() ;
							}
							
							frame.dispose() ;
							System.exit(0) ;
						}
					}			
			) ;			
		}
		catch(Exception e) {
			e.printStackTrace();
		}
					
		videoPlayer = new BasicVideoPlayer(this) ;
	}
	
	protected void setupLogicLinks() {		
		videoSizeMonitorThread = new VideoSizeMonitorThread() ;
		videoSizeMonitorThread.setDaemon(true) ;
		videoSizeMonitorThread.start() ;
			
		//初始化组件信息。
		button.registerLinks(this) ;
	}
	
	public void playVideo(String videoFile){
		videoPlayer.playVideo(videoFile, null) ;
		
		videoPlayer.getVideoClipInfo().addClipInfoChangeListener(new ClipInfoChangeListener(){

			public void updateClipInfo(ClipInfoChangeEvent evt) {
				if(evt.match("ANS_width") || evt.match("ANS_height")){
					VideoClipInfo ci = videoPlayer.getVideoClipInfo() ;
					int orginalWidth = ci.getWidth() ;
					int orginalHeight = ci.getHeight() ;
					
					if(orginalWidth > 0 && orginalHeight > 0){
						resizeVideoWindow(orginalWidth, orginalHeight) ;
					}
				}
			}			
		}) ;
	}
		
	protected void onFrameResizing(Dimension oldSize){
		int width = frame.getWidth() ;
		int height = frame.getHeight() - no_cavas_height ;
		
		if(width < 1 || height < 1){
			return ;
		}		
		
		//根据原始视频大小调整播放器大小。
		VideoClipInfo ci = videoPlayer.getVideoClipInfo() ;
		int orginalWidth = ci.getWidth() ;
		int orginalHeight = ci.getHeight() ;
		
		if(orginalWidth > 1 || orginalHeight > 1){
			//successed to retrieve the video size.
			
			float w_scale = (float)width / orginalWidth ;
			float h_scale = (float)height / orginalHeight ;
			
			float s_scale = Math.min(w_scale, h_scale) ;
			width  = (int)(orginalWidth * s_scale) ;
			height = (int)(orginalHeight * s_scale) ;
		}
		
		f.setSize(width, height);		
		System.out.println("resize to:" + width + "," + height + "; frame is:" + frame.getWidth() + "," + frame.getHeight());
		
		frame.validate() ;
		frame.repaint() ;
	}
	
	/**设置播放窗口大小*/
	protected void resizeVideoWindow(int width, int height){
		if(width < 1 || height < 1){
//			f.setVisible(false) ;
			return ;
		}		
		
		//根据原始视频大小调整播放器大小。
		VideoClipInfo ci = videoPlayer.getVideoClipInfo() ;
		int orginalWidth = ci.getWidth() ;
		int orginalHeight = ci.getHeight() ;
		
		if(orginalWidth > 1 || orginalHeight > 1){
			//successed to retrieve the video size.
			
			float w_scale = (float)width / orginalWidth ;
			float h_scale = (float)height / orginalHeight ;
			
			float s_scale = Math.min(w_scale, h_scale) ;
			width  = (int)(orginalWidth * s_scale) ;
			height = (int)(orginalHeight * s_scale) ;
		}
		
		videoSizeMonitorThread.autoResizeStart() ;
		f.setSize(width, height);
		System.out.println("auto resize to:" + width + "," + height + "; frame is:" + frame.getWidth() + "," + frame.getHeight());
		
		frame.setSize(f.getWidth(), f.getHeight() + no_cavas_height) ;
		
		frame.validate() ;
		frame.repaint() ;
		videoSizeMonitorThread.autoResizeEnd() ;
	}
	
	
	public static void main(String arg[]){
		FleetPlayer fp = new FleetPlayer() ;
		System.out.println("start playing....");
//		fp.playVideo("D:/tmp/us.avi") ;
		fp.playVideo("D:/tmp/streams/testmov.mov") ;
		fp.videoPlayer.seekTo(5);
//		System.out.println("pos:" + fp.videoPlayer.pause());
	}	
	
	private class VideoSizeMonitorThread extends Thread{
		private Dimension dnow ;
		private boolean noMonitor ;
		
		public void autoResizeStart(){
			noMonitor = true ;
		}
		
		public void autoResizeEnd(){
			dnow = frame.getSize() ;
			noMonitor = false ;
		}
		
		public VideoSizeMonitorThread(){
			this.dnow = frame.getSize() ;
		}
		
		public void run(){
			while(true){
				try{
					synchronized (this) {
						wait(100) ;
						if(!noMonitor){
							Dimension c = frame.getSize() ;
							if(!c.equals(dnow)){
								FleetPlayer.this.onFrameResizing(dnow) ;
								dnow = c ;
							}
						}
					}	
				}catch(Exception e){}
			}
		}
	}

}
